﻿#include "AbstractSliderPropertyEditEntry.h"

#include <QAbstractSlider>
#include "ObjectPropertyEditEntryFactory.h"
REGISTER_PROPERTY_EDIT_ENTRY(AbstractSlider)

#include "IntegerEditEntry.h"
#include "BooleanEditEntry.h"
#include "EnumEditEntry.h"

struct AbstractSliderPropertyEditEntry::AbstractSliderPropertyEditEntryPrivate {
	QPointer<QAbstractSlider> object;
	
	QPointer<IntegerEditEntry> entryMinimum;
	QPointer<IntegerEditEntry> entryMaximum;
	QPointer<IntegerEditEntry> entrySingleStep;
	QPointer<IntegerEditEntry> entryPageStep;
	QPointer<IntegerEditEntry> entryValue;
	QPointer<IntegerEditEntry> entrySliderPosition;
	QPointer<BooleanEditEntry> entryTracking;
	QPointer<EnumEditEntry> entryOrientation;
	QPointer<BooleanEditEntry> entryInvertedApperance;
	QPointer<BooleanEditEntry> entryInvertedControlls;
};

AbstractSliderPropertyEditEntry::~AbstractSliderPropertyEditEntry() { }

AbstractSliderPropertyEditEntry::AbstractSliderPropertyEditEntry(QWidget *parent) : PropertyEditEntry{parent} {
	p.reset(new AbstractSliderPropertyEditEntryPrivate);
	initUi();
	retranslateUi();
}

void AbstractSliderPropertyEditEntry::setObject(void* object) {
	p->object = static_cast<QAbstractSlider*>(object);
}

void AbstractSliderPropertyEditEntry::onTick() {
	if (!p->object || !isVisible()) {
		return;
	}

	p->entryMinimum->setValue(p->object->minimum());
	p->entryMaximum->setValue(p->object->maximum());
	p->entrySingleStep->setValue(p->object->singleStep());
	p->entryPageStep->setValue(p->object->pageStep());
	p->entryValue->setValue(p->object->value());
	p->entrySliderPosition->setValue(p->object->sliderPosition());
	p->entryTracking->setValue(p->object->hasTracking());
	p->entryOrientation->setValue(p->object->orientation());
	p->entryInvertedApperance->setValue(p->object->invertedAppearance());
	p->entryInvertedControlls->setValue(p->object->invertedControls());
}

void AbstractSliderPropertyEditEntry::initUi() {
	p->entryMinimum = new IntegerEditEntry;
	appendChildEntry(p->entryMinimum);
	connect(p->entryMinimum, &IntegerEditEntry::valueChanged, [=](int value) {
		p->object->setMinimum(value);
	});

	p->entryMaximum = new IntegerEditEntry;
	appendChildEntry(p->entryMaximum);
	connect(p->entryMaximum, &IntegerEditEntry::valueChanged, [=](int value) {
		p->object->setMaximum(value);
	});

	p->entrySingleStep = new IntegerEditEntry;
	appendChildEntry(p->entrySingleStep);
	connect(p->entrySingleStep, &IntegerEditEntry::valueChanged, [=](int value) {
		p->object->setSingleStep(value);
	});

	p->entryPageStep = new IntegerEditEntry;
	appendChildEntry(p->entryPageStep);
	connect(p->entryPageStep, &IntegerEditEntry::valueChanged, [=](int value) {
		p->object->setPageStep(value);
	});

	p->entryValue = new IntegerEditEntry;
	appendChildEntry(p->entryValue);
	connect(p->entryValue, &IntegerEditEntry::valueChanged, [=](int value) {
		p->object->setValue(value);
	});

	p->entrySliderPosition = new IntegerEditEntry;
	appendChildEntry(p->entrySliderPosition);
	connect(p->entrySliderPosition, &IntegerEditEntry::valueChanged, [=](int value) {
		p->object->setSliderPosition(value);
	});

	p->entryTracking = new BooleanEditEntry;
	appendChildEntry(p->entryTracking);
	connect(p->entryTracking, &BooleanEditEntry::valueChanged, [=](bool value) {
		p->object->setTracking(value);
	});

	p->entryOrientation = new EnumEditEntry;
	appendChildEntry(p->entryOrientation);
	p->entryOrientation->setMetaEnum(QMetaEnum::fromType<Qt::Orientation>());
	connect(p->entryOrientation, &EnumEditEntry::valueChanged, [=](int value) {
		p->object->setOrientation(Qt::Orientation(value));
	});

	p->entryInvertedApperance = new BooleanEditEntry;
	appendChildEntry(p->entryInvertedApperance);
	connect(p->entryInvertedApperance, &BooleanEditEntry::valueChanged, [=](bool value) {
		p->object->setInvertedAppearance(value);
	});

	p->entryInvertedControlls = new BooleanEditEntry;
	appendChildEntry(p->entryInvertedControlls);
	connect(p->entryInvertedControlls, &BooleanEditEntry::valueChanged, [=](bool value) {
		p->object->setInvertedControls(value);
	});
}

void AbstractSliderPropertyEditEntry::retranslateUi() {
	setTitle(tr("QAbstractSlider"));

	p->entryMinimum->setTitle(tr("minimum"));
	p->entryMaximum->setTitle(tr("maximum"));
	p->entrySingleStep->setTitle(tr("singleStep"));
	p->entryPageStep->setTitle(tr("pageStep"));
	p->entryValue->setTitle(tr("value"));
	p->entrySliderPosition->setTitle(tr("sliderPosition"));
	p->entryTracking->setTitle(tr("tracking"));
	p->entryOrientation->setTitle(tr("orientation"));
	p->entryInvertedApperance->setTitle(tr("invertedApperance"));
	p->entryInvertedControlls->setTitle(tr("invertedControlls"));
}
